package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ConstantPoolName_Info;

public class Structure_CP_NameInfo extends Structure_CP_Constant {

	ConstantPoolName_Info myNameInfo;
	
	public Structure_CP_NameInfo(ConstantPoolName_Info myNameInfo) {
		this.myNameInfo=myNameInfo;	
	}

	public int getMemorySize() {
		int size;
		
		size=BYTE_WIDTH;	// Type
		size+=SHORT_WIDTH;	// class name index
		size+=SHORT_WIDTH;	// descriptor index
		return(size);
	}

	public int getNameIndex() {
		return(myNameInfo.getNameIndex());	
	}

	public int getDescriptorIndex() {
		return(myNameInfo.getDescriptorIndex());	
	}

	protected String getMemoryMapAsString() {		
//		returnString=getAdressAsHexString(baseAdress);
//		returnString+=" : (name info)   | size - " + new Formatter().format("%3d",getMemorySize());
//		returnString+="| name - "+myNameInfo.getNameIndex();
//		returnString+=" descriptor : "+myNameInfo.getDescriptorIndex()+"\n";
		
		Formatter fmt = new Formatter();
		return "" + fmt.format("%s |%14s | size - %3d | name - %3d | descriptor - %3d\n", 
				getAdressAsHexString(baseAdress), 
				"(name info)", 
				getMemorySize(), 
				myNameInfo.getNameIndex(),
				myNameInfo.getDescriptorIndex());
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.put((byte)myNameInfo.getType());
		fileData.putShort((short)myNameInfo.getNameIndex());
		fileData.putShort((short)myNameInfo.getDescriptorIndex());
	}

}
